import { dynamic } from 'umi';
import request from 'umi-request';
import { MenuDataItem } from '@ant-design/pro-layout';

// 获取配置数据
export const getSetting = () => {
  return request.post('/system/setting');
}

// 获取菜单
export const getResource = () => {
  return request.post('/system/resource');
};

// 检测登录
export const isLogin = () => {
  return request.post('/system/isLogin');
}

// 合并菜单到路由信息
export const mergeRoure = (routes: any, route: MenuDataItem) => {
  // 构造路由
  const forRoute = (dataArray: any) => {
    if (dataArray == null) {
      return;
    }
    dataArray.forEach((item: any) => {
      if (item.hasOwnProperty('component')) {
        let addRoute = { ...item };
        addRoute.component = dynamic({
          loader: () => import(`@/pages/${item.component}`),
        });
        routes[0].routes.unshift(addRoute);
      } else if (item.hasOwnProperty('routes')) {
        forRoute(item.routes);
      }
    });
  };
  forRoute(route);
};
